<?php

namespace App\Http\Controllers\Api;

use App\Api\Filters\BooksFilters;
use App\Http\Requests\BookCreateRequest;
use App\Models\Book;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

use  App\Http\Resources\Book as BookCollection;


class BooksController extends ApiController
{

    public function __construct()
    {
        $this->middleware('auth:api')->only([
            'store',
            'destroy'
        ]);
    }

    protected function getFilterBooks(BooksFilters $filters,callable $callback = null){


        $limit = Input::get('limit') ?: 20;

        $builder = Book::query()->normal()->filter($filters);

        if ($callback){
            $callback($builder);
        }

        $books = $builder->paginate($limit);

        $books->appends(Input::except('page'));

        return $books;

    }

    /**
     *
     * @api {get} /books 获取所有书籍
     * @apiName Books all
     * @apiGroup Books
     *
     * @apiParam {String} [limit] 限制的条数
     * @apiParam {String} [page] 当前的页数
     * @apiParam {String} [gender] 不传为全部,0=>男频,1=>女频
     * @apiParam {String} [status] 书籍状态,0=>已完结,1=>连载中
     * @apiParam {String} [wordCount] 书籍字数,0 =>不限,1 => 0 - 30 万,2 =>30万-50万,3 =>50万-100万,4 =>100万-200万,5 =>200万以上
     * @apiParam {String} [type] 书籍类型id
     *
     * @apiVersion 2.0.0
     *
     * @apiSampleRequest /api/books
     *
     *
     */
    public function index(BooksFilters $filters){

        $books = $this->getFilterBooks($filters);

        return BookCollection::collection($books);
    }



    /**
     *
     * @api {delete} books/:book_id 删除书籍
     * @apiName Book delete
     * @apiGroup Book
     *
     * @apiParam {String} book_id 删除书籍的id
     * @apiVersion 1.0.0
     *
     * @apiSampleRequest /api/books/:book_id
     *
     */
    public function destroy(Book $book){

        $this->authorize('destroy',$book);

        $book->delete();

        return $this->success('删除成功');

    }


    /**
     *
     * @api {get} /books/search 搜索书籍
     * @apiName Books search
     * @apiGroup Books
     *
     * @apiParam {String} [qw] 搜索的关键字
     * @apiParam {String} [limit] 限制的条数
     * @apiParam {String} [page] 当前的页数
     *
     * @apiVersion 2.0.0
     *
     * @apiSampleRequest /api/books/search
     *
     *
     */
    public function search(){

        $qw = Input::get('qw');
        $limit = Input::get('limit') ?: 20;

        $books = Book::where('name','like','%'.$qw.'%')
        ->paginate($limit);

        return BookCollection::collection($books);

    }



    /**
     *
     * @api {post} /books 创建书籍
     * @apiName Books create
     * @apiGroup Books
     *
     * @apiParam {String} name 书籍的名字
     * @apiParam {String} isopen 书籍的公开状态
     * @apiParam {String} status 书籍的状态,0为已完结1为连载2为已删除
     * @apiParam {String} gender 男频女频,0为男行1为女型
     * @apiParam {String} type 书籍的分类id
     * @apiParam {String} labels[] 书籍的标签
     * @apiParam {String} [introduction] 书籍的简介
     * @apiParam {String} cover 书籍的封面图片地址
     *
     * @apiVersion 2.0.0
     *
     * @apiSampleRequest /api/books
     *
     *
     */
    public function store(BookCreateRequest $request){


        $user = Auth::user();
        $labels = json_encode($request->labels);
        $request->offsetUnset('labels');
        $request->offsetSet('labels',$labels);

        $request->offsetSet('author',$user->nickname);
        $user->books()->create($request->all());


        return $this->message('创建成功');


    }


    /**
     *
     * @api {get} /books/hot_list 热度排行
     * @apiName Books hot_list
     * @apiGroup Books
     *
     * @apiParam {String} [limit] 限制的条数
     * @apiParam {String} [page] 当前的页数
     * @apiParam {String} [gender] 不传为全部,0=>男频,1=>女频
     * @apiParam {String} [status] 书籍状态,0=>已完结,1=>连载中
     * @apiParam {String} [wordCount] 书籍字数,0 =>不限,1 => 0 - 30 万,2 =>30万-50万,3 =>50万-100万,4 =>100万-200万,5 =>200万以上
     * @apiParam {String} [type] 书籍类型id
     *
     * @apiVersion 1.0.0
     *
     * @apiSampleRequest /api/books/hot_list
     *
     *
     */
    public function hotList(BooksFilters $filters){

        $books = $this->getFilterBooks($filters,function ($builder){
            $builder->clearOrders()->orderBy('read_counts','desc');
        });

        return BookCollection::collection($books);

    }



    /**
     *
     * @api {get} /books/hot_books 热门书籍
     * @apiName Books hot_books
     * @apiGroup Books
     *
     * @apiParam {String} [limit] 限制的条数
     * @apiParam {String} [page] 当前的页数
     * @apiParam {String} [gender] 不传为全部,0=>男频,1=>女频
     * @apiParam {String} [status] 书籍状态,0=>已完结,1=>连载中
     * @apiParam {String} [wordCount] 书籍字数,0 =>不限,1 => 0 - 30 万,2 =>30万-50万,3 =>50万-100万,4 =>100万-200万,5 =>200万以上
     * @apiParam {String} [type] 书籍类型id
     *
     * @apiVersion 1.0.0
     *
     * @apiSampleRequest /api/books/hot_books
     *
     *
     */
    public function hotBooks(BooksFilters $filters){

        $books = $this->getFilterBooks($filters,function ($builder){
            $builder->clearOrders()->orderBy('collection_counts','desc');
        });

        return BookCollection::collection($books);


    }


    /**
     *
     * @api {get} books/hot_update 热门更新
     * @apiName Books hot_update
     * @apiGroup Books
     *
     * @apiParam {String} [limit] 限制的条数
     * @apiParam {String} [page] 当前的页数
     * @apiParam {String} [gender] 不传为全部,0=>男频,1=>女频
     * @apiParam {String} [status] 书籍状态,0=>已完结,1=>连载中
     * @apiParam {String} [wordCount] 书籍字数,0 =>不限,1 => 0 - 30 万,2 =>30万-50万,3 =>50万-100万,4 =>100万-200万,5 =>200万以上
     * @apiParam {String} [type] 书籍类型id
     *
     * @apiVersion 1.0.0
     *
     * @apiSampleRequest /api/books/hot_update
     *
     */
    public function hotUpdate(BooksFilters $filters){


        $books = $this->getFilterBooks($filters,function ($builder){
            $builder->where('status',1)
                ->clearOrders()
                ->orderBy('read_counts','desc')
                ->orderBy('last_modify_time','desc');
        });
        return BookCollection::collection($books);

    }


    /**
     *
     * @api {get} books/hot_complete 热门完结
     * @apiName Books hot_complete
     * @apiGroup Books
     *
     * @apiParam {String} [limit] 限制的条数
     * @apiParam {String} [page] 当前的页数
     * @apiParam {String} [gender] 不传为全部,0=>男频,1=>女频
     * @apiParam {String} [status] 书籍状态,0=>已完结,1=>连载中
     * @apiParam {String} [wordCount] 书籍字数,0 =>不限,1 => 0 - 30 万,2 =>30万-50万,3 =>50万-100万,4 =>100万-200万,5 =>200万以上
     * @apiParam {String} [type] 书籍类型id
     *
     * @apiVersion 1.0.0
     *
     * @apiSampleRequest /api/books/hot_complete
     *
     */
    public function hotComplete(BooksFilters $filters){


        $books = $this->getFilterBooks($filters,function ($builder){
            $builder->where('status',0)
                ->clearOrders()
                ->orderBy('read_counts','desc')
                ->orderBy('last_modify_time','desc');
        });

        return BookCollection::collection($books);


    }





    /**
     *
     * @api {get} books/relative 搜索这本书的人还看
     * @apiName Books relative
     * @apiGroup Books
     *
     * @apiParam {String} [limit] 限制的条数
     * @apiParam {String} [page] 当前的页数
     * @apiParam {String} [qw] 搜索的关键字
     *
     *
     * @apiVersion 1.0.0
     *
     * @apiSampleRequest /api/books/relative
     *
     */
    public function relative(){
        $qw = Input::get('qw');

        $limit = Input::get('limit') ?: 20;

        $books = Book::normal()->where('name','like','%'.$qw.'%')->limit(3)->get();

        $types = collect($books)->map(function ($book){
            return $book->bookType->id;
        })->toArray();

        $books = Book::whereIn('type',$types)
            ->orderBy('read_counts','desc')
            ->paginate($limit);

        return BookCollection::collection($books);

    }

    /**
     *
     * @api {get} books/new_list 新书上架
     * @apiName Books new_list
     * @apiGroup Books
     *
     * @apiParam {String} [limit] 限制的条数
     * @apiParam {String} [page] 当前的页数
     * @apiParam {String} [gender] 不传为全部,0=>男频,1=>女频
     * @apiParam {String} [status] 书籍状态,0=>已完结,1=>连载中
     * @apiParam {String} [wordCount] 书籍字数,0 =>不限,1 => 0 - 30 万,2 =>30万-50万,3 =>50万-100万,4 =>100万-200万,5 =>200万以上
     * @apiParam {String} [type] 书籍类型id
     *
     * @apiVersion 1.0.0
     *
     * @apiSampleRequest /api/books/new_list
     *
     */
    public function newList(BooksFilters $filters){


        $books = $this->getFilterBooks($filters,function ($builder){
            $builder->clearOrders()
                ->orderBy('create_time','desc');
        });

        return BookCollection::collection($books);
    }

}
